package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.utils.CurrentHolder;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;



    /**
     * 分页查询菜品列表
     * @param
     * @return
     */
    @Override
    public PageResult pageDish(Integer page, Integer pageSize, String name,Integer categoryId, Integer status) {
        //分页查询
        PageHelper.startPage(page, pageSize);
        //条件查询数据
       List<DishVO> dishList = dishMapper.getDishListAdmin(categoryId, name, status);
       //这是多态可以强转 这是PageHelper中提供的page类型的集合 把查出的菜品数据放入集合
        Page<DishVO> page1 = (Page<DishVO>) dishList;
        //这个集合中就有查询出来的 总数数据
        return new PageResult(page1.getTotal(), page1.getResult());
    }

    /**
     * 根据分类id条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        // 只查询状态为启用的菜品
        dish.setStatus(StatusConstant.ENABLE);
        //根据分类id查询菜品信息
        List<Dish> dishList =dishMapper.getDishList(dish);
        log.info("查询到的菜品数据为：{}", dishList);
        // 2. 转换为DishVO并设置关联属性
        List<DishVO> result = dishList.stream().map(d -> {
            DishVO dishVO = new DishVO();
            // 复制基本属性
            BeanUtils.copyProperties(d, dishVO);
            //查询分类名称 并存入集合
            Category category = categoryMapper.getCategoryName(d.getCategoryId());
            if (category != null) {
                dishVO.setCategoryName(category.getName());
            }

            //查询菜品口味并放入返回集合中
            List<DishFlavor> flavors = dishFlavorMapper.flavorList(d.getId());
            dishVO.setFlavors(flavors);
            return dishVO;
        }).collect(Collectors.toList());
        return result;
    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        try {
            //新增菜品
            Dish dish = new Dish();
            dish.setCreateTime(LocalDateTime.now());
            dish.setUpdateTime(LocalDateTime.now());
            //设置用户id
            dish.setCreateUser(CurrentHolder.getCurrentId());
            dish.setUpdateUser(CurrentHolder.getCurrentId());
            //把DTO对象中的属性复制给Dish对象
            BeanUtils.copyProperties(dishDTO,dish);
            //插入菜品数据
            dishMapper.save(dish);
            System.out.println( dish);

            //获取菜品id
            Long dishId = dish.getId();
            //从前端传过来的数据中翻获取菜品口味数据，并放入一个新的集合中
            List<DishFlavor> flavors  = dishDTO.getFlavors();
            if (dishId != null){
                //给得到的菜品口味集合遍历、设置菜品id
                flavors.forEach(dishFlavor ->
                        dishFlavor.setDishId(dishId)
                );
                //新增口味
                dishFlavorMapper.saveBatch(flavors);
            }
        } finally {
            //日志写
        }
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<String> ids) {
        //删除菜品
        dishMapper.delete(ids);
        //删除菜品口味
        dishFlavorMapper.deleteFlavor(ids);
    }

    /**
     * 根据id查询菜品和对应的口味
     * @param dishById
     * @return
     */
    @Override
    public DishVO getById(Dish dishById) {
        //查询菜品数据
        DishVO dishVO = dishMapper.getById(dishById.getId());
        log.info("查询到的菜品数据为：{}", dishVO);
        Long dishId = dishVO.getId();
        //查询口味数据
        List<DishFlavor> flavors = dishFlavorMapper.getDishFavor(dishId);
        if (flavors != null){
            dishVO.setFlavors(flavors);
        }
        return dishVO;

    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        //修改菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(CurrentHolder.getCurrentId());
        dishMapper.update(dish);
        Long dishId = dish.getId();

        //修改口味信息
        //删除当前菜品对应的口味数据
        dishFlavorMapper.deleteFlavorId(dishId);
        //添加新的口味数据 设置dish id
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors){
            flavor.setDishId(dishId);
        }
        if (!CollectionUtils.isEmpty( flavors)) {
            dishFlavorMapper.saveBatch(flavors);
        }
    }

    /**
     * 启售、停售菜品
     * @param status
     * @param id
     */
    @Override
    public void setStatus(String status, String id) {
        //设置更新时间和更新者
        Dish dish = new Dish();
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(CurrentHolder.getCurrentId());
        dish.setId(Long.valueOf(id));
        dishMapper.update(dish);
        //修改菜品状态
       dishMapper.setStatus(status,id);
    }


}
